home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / QUEUE.CPP < prev    next >
C/C++ Source or Header  |  1993-04-27  |  2KB  |  102 lines

  1. #include "arrayob.h"
  2. #include "queue.h"
  3.  
  4. #define THIS Queue
  5. #define BASE Object
  6. DEFINE_CLASS(Queue,Object);
  7.  
  8. void Queue::state() const  //diagnostic
  9. {
  10.     cout << "Queue capacity: " << capacity() << "\n";
  11.     cout << "Queue size: " << size() << "\n";
  12.     cout << "Queue *pContents: " << *pContents << "\n";
  13.     cout << "readPosition: " << readPosition << "\n";
  14.     cout << "writePosition: " << writePosition << "\n";
  15. }
  16.  
  17. unsigned Queue::capacity() const
  18. {
  19.     return pContents->capacity();
  20. }
  21.  
  22. void Queue::makeRoomForWrite()
  23. {
  24.     if (readPosition==0)
  25.     {
  26.         //cout << "resizing array\n"; // diagnostic
  27.         int i = capacity();
  28.         pContents->reSize(size()+QUEUE_EXPANSION_INCREMENT);
  29.  
  30.             // -gmv  An error will occur if you try to print
  31.             // the *pContents array without setting all
  32.             // newly created elements to nil.  It seems
  33.             // this should be a responsibility of the
  34.             // ArrayOb class.
  35.         for(; i<capacity(); i++)
  36.             (*pContents)[i] = (Object*)nil;
  37.     }
  38.     else
  39.     {
  40.         // move all object to beginning of array
  41.         unsigned contentsSize = writePosition - readPosition;
  42.         for (int i=0; i<contentsSize; i++)
  43.         {
  44.             (*pContents)[i] = (*pContents)[i+readPosition];
  45.         }
  46.         readPosition=0;
  47.         writePosition=contentsSize;
  48.     }
  49. }
  50.  
  51. unsigned Queue::size() const
  52. {
  53.     return writePosition - readPosition;
  54. }
  55.  
  56.  
  57. void Queue::printOn(ostream& strm) const
  58. {
  59.  
  60.     strm << className() << "[\n";
  61.     for (int i=readPosition; i<writePosition; i++)
  62.     {
  63.         if (i>0) strm << "\n";
  64.         (*pContents)[i]->printOn(strm);
  65.     }
  66.     strm << "]\n";
  67. }
  68.  
  69. Queue::Queue(int sz)
  70. {
  71.     pContents = new ArrayOb(sz);
  72.     readPosition = writePosition = 0;
  73. }
  74.  
  75. Object* Queue::next()
  76. {
  77.     if (readPosition==writePosition)
  78.     {
  79.         cerr << "Error, Queue::next(): queue empty\n";
  80.         //exit(1);
  81.     }
  82. #if 1
  83.     // better debugging info
  84.     Object *p = (*pContents)[readPosition];
  85.     (*pContents)[readPosition++] = (Object*)nil;
  86.     return p;
  87. #else
  88.     // more efficient,
  89.     return (*pContents)[i];
  90. #endif
  91.  
  92. }
  93.  
  94. Object* Queue::nextPut(const Object& ob)
  95. {
  96.     if (writePosition>=capacity())
  97.         makeRoomForWrite();
  98.     (*pContents)[writePosition++] = (Object*)&ob;
  99.     return (Object*)&ob; // error ?
  100. }
  101.  
  102.